package com.liyueheng.app.dataWarehouseDetail.rfm

import com.liyueheng.util.{ConfigLoader, SaveAsTable, SparkConf}
import org.apache.spark.sql.functions._

object LastPurchaseDate {
  def calcR(): Unit = {
    println("------------------ 计算最近购买日期分数 -----------------")
    val spark = SparkConf.createSparkSession("RFM-R")
    val dwd = ConfigLoader.getString("databases.dwd")
    val dws = ConfigLoader.getString("databases.dws")

    val df = spark.table(s"$dwd.user_act")
      .filter("act_type = 2")
      .groupBy("user")
      .agg(max("act_date").alias("last_purchase_date"))

    SaveAsTable.saveAsTable(df, s"$dws.detail_rfm_last_purchase_date")
    SparkConf.stopSparkSession(spark)
  }
}
